<?php
declare ( strict_types = 0 );
namespace app\controller;

use think\facade\View;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\Cache;
use app\model\MenuModel;
use app\model\UserModel;
use app\model\DeptModel;

//菜单管理控制器

class Menu {

    //常用数据表内容 及常用字典
    var $top_menu = [];
    var $dept = [];
    var $users = [];
    var $group = [];

    var $status = array( '1'=>'正常(Using)', '2'=>'停用(Stoped)' );

    public function __construct() {
        $um = new UserModel;
        $this->users = $um->getAll();

        $dm = new DeptModel;
        $this->dept = $dm->getAll();

        $list = Db::table( 'user_group' )->select()->toArray();
        foreach ( $list as $k=>$v ) {
            $this->group[ $v[ 'id' ] ] = $v;
        }

    }

    //类别

    public function index() {

        $menu1 = Db::query( 'SELECT * FROM `menu` where 1 order by sort' );
        //菜单处理, 子菜单排序
        $menuDo = [];

        foreach ( $menu1 as $kv ) {

            if ( $kv[ 'fid' ] == 0 ) {
                $kv[ 'class' ] = 'top_menu';
                $kv[ 'creater_name' ] = $this->users[ $kv[ 'creater' ] ][ 'name' ];
                $kv[ 'status_name' ] = $this->status[ $kv[ 'status' ] ];
                $menuDo[] = $kv;

                foreach ( $menu1 as $kv1 ) {
                    if ( $kv1[ 'fid' ] == $kv[ 'id' ] ) {
                        $kv1[ 'name' ] = '|___'.$kv1[ 'name' ];
                        $kv1[ 'class' ] = 'second_menu fid_'.$kv[ 'id' ];
                        $kv1[ 'creater_name' ] = $this->users[ $kv1[ 'creater' ] ][ 'name' ];
                        $kv1[ 'status_name' ] = $this->status[ $kv1[ 'status' ] ];
                        $menuDo[] = $kv1;
                    }
                }
            }
        }

        View::assign( [ 'data'=>$menuDo ] );
        return View::fetch();
    }

    //菜单编辑方法

    public function edit() {
        $all = Request::param();
        $id = $all[ 'id' ];

        if ( empty( $id ) ) return 'Invalid cat id!' ;
        $model = new MenuModel();

        //传入内容, 保存数据并跳转
        if ( $id>0 && !empty($all['name']) ) {
            $all['name']=str_replace('|','',$all['name']);
            $all['name']=str_replace('_','',$all['name']);
            if ( !empty( $all[ 'group_id' ] ) ) {
                $all[ 'group_id' ] = implode( ',', $all[ 'group_id' ] );
            }
            $all[ 'create_time' ] = date( 'Y-m-d H:i:s' );
            Db::table( 'menu' )->save( $all );
            header( 'location:/index.php/menu/index' );
            
        }


        //不传内容只有id, 查出本条数据 并进入编辑
        $row = $model->find( $id );

        //权限组的处理
        $g_list = explode( ',', $row[ 'group_id' ] );
        if ( !$g_list ) $g_list = [];
        $g_html = '';
        foreach ( $this->group as $k=>$group ) {
            if ( in_array( $k, $g_list ) ) {
                $g_html .= '<div class="group_item group_item_checked"><input type="checkbox" id="check'.$k.'" name="group_id[]" value="'.$k.'" checked />
      <label for="check'.$k.'">'.$group[ 'name' ].'</label></div>';
            } else {
                $g_html .= '<div class="group_item"><input type="checkbox" id="check'.$k.'" name="group_id[]" value="'.$k.'" />
      <label for="check'.$k.'">'.$group[ 'name' ].'</label></div>';
            }
        }

        //print_r( $g_html );
        View::assign( [
            'top_menu'=>$model->top_menu(),
            'data'=>$row,
            'group'=>$this->group,
            'g_html'=>$g_html
        ] );
        return View::fetch( 'menu_edit' );
    }

    //分类新增

    public function menu_add() {
        $uid = Session::get( 'logonid' );
        if ( !$uid ) return header( 'location:/index.php/login' );

        $all = Request::param();
        $model = new MenuModel();
        if ( $all ) {
            if ( !empty( $all[ 'id' ] ) ) return 'It is not a new item!';

            if ( !$all[ 'name' ] ) return 'Invalid item name!';

            $row = $model::where( 'name', $all[ 'name' ] )->find();
            if ( !empty( $row ) ) return '菜单项已存在! item exist! ['. $all[ 'name' ].']';
            if ( $all[ 'group_id' ] ) {
                $all[ 'group_id' ] = implode( ',', $all[ 'group_id' ] );
            }

            $all[ 'creater' ]  = Session::get( 'logonid' ) ;
            $all[ 'create_time' ]  = date( 'Y-m-d H:i:s', time() );

            $model->insert( $all );
            header( 'location:/index.php/menu' );
        }

        //权限组的处理

        $g_html = '';
        foreach ( $this->group as $k=>$group ) {
            $g_html .= '<div class="group_item"><input type="checkbox" id="check'.$k.'" name="group_id[]" value="'.$k.'" />
      <label for="check'.$k.'">'.$group[ 'name' ].'</label></div>';
        }

        View::assign( [ 'top_menu'=>$model->top_menu(), 'g_html'=>$g_html ] );
        return View::fetch( 'menu_add' );
    }

    

}
